package com.drama.api.sys.message.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.message.domain.vo.CompanyMessageReadVO;
import com.drama.api.company.message.domain.vo.CompanyMessageVO;
import com.drama.api.sys.message.dao.SysMessageDao;
import com.drama.api.sys.message.dao.SysMessageRoleDao;
import com.drama.api.sys.message.domain.po.SysMessagePO;
import com.drama.api.sys.message.domain.po.SysMessageRolePO;
import com.drama.api.sys.message.domain.vo.SysMessageAllSearchVO;
import com.drama.api.sys.message.domain.vo.SysMessageSearchVO;
import com.drama.api.sys.message.service.ISysMessageService;
import com.drama.api.sys.role.dao.SysRoleDao;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class SysMessageServiceImpl implements ISysMessageService {

    @Autowired
    private SysMessageDao messageDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysMessageRoleDao sysMessageRoleDao;

    @Override
    public List<CompanyMessageVO> getList(SysMessageSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getUserId())){
            searchVO.setUserId((String) StpUtil.getLoginId());
        }
        return messageDao.getList(searchVO);
    }

    @Override
    public List<CompanyMessageVO> getAllList(SysMessageAllSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getUserId())){
            searchVO.setUserId((String) StpUtil.getLoginId());
        }
        return messageDao.getAllList(searchVO);
    }

    @Override
    public void read(CompanyMessageReadVO vo) {
        SysMessagePO companyMessagePO=messageDao.selectByPrimaryKey(vo.getId());
        if(companyMessagePO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "不存在需要标记的信息！");
        }
        companyMessagePO.setMsgState(1);
        companyMessagePO.preUpdate();
        int i = messageDao.updateByPrimaryKeySelective(companyMessagePO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void all_read() {
        messageDao.all_read((String) StpUtil.getLoginId());
    }

    @Override
    public void addSysMsg( String msgTitle, String msgDesc,String msgUrl, Integer msgType) {
        SysMessagePO sysMessagePO=new SysMessagePO();
        sysMessagePO.setMsgTitle(msgTitle);
        sysMessagePO.setMsgDesc(msgDesc);
        sysMessagePO.setMsgState(2);
        sysMessagePO.setMsgType(msgType);
        sysMessagePO.setMsgUrl(msgUrl);
        sysMessagePO.preInsert();
        messageDao.insertSelective(sysMessagePO);
        List<String> roleIds= sysRoleDao.getMsgRole(msgType);
        if(roleIds!=null&&roleIds.size()>0){
            for (String roleId :roleIds){
                SysMessageRolePO sysMessageRolePO=new SysMessageRolePO();
                sysMessageRolePO.setMsgId(sysMessagePO.getId());
                sysMessageRolePO.setRoleId(roleId);
                sysMessageRoleDao.insertSelective(sysMessageRolePO);
            }
        }
    }


}